import 'dart:async';

import 'package:flutter/material.dart';
import 'package:jtsell_app/entity/mall_goods_entity.dart';
import 'package:jtsell_app/network/http_dispense.dart';
import 'package:jtsell_app/provider/test_stream_provider_model.dart';
import 'package:provider/provider.dart';

class StreamProviderPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return StreamProvider<List<MallGoodsEntity>>.controller(
        builder: (BuildContext context) => streamProvideModel.streamCtrl, child: CountWidget1());
  }
}

class CountWidget1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Column(
      children: <Widget>[ButtonWidget1(), TextWidget1()],
    ));
  }
}

class ButtonWidget1 extends StatelessWidget {
  int count = 0;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: FlatButton.icon(
          icon: Icon(Icons.add),
          label: Text("获取菜单名"),
          onPressed: () {
            streamProvideModel.streamCtrl
                .addStream(Stream.fromFuture(HttpDispense.getMallGoodsList({"page": ++count})));
          }),
    );
  }
}

class TextWidget1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Consumer<List<MallGoodsEntity>>(
          builder: (BuildContext context, List<MallGoodsEntity> value, Widget child) {
        if (value != null) {
          value.forEach((item) {
            print(item.goodsName);
          });

          String listString = value.map((item) => item.goodsName).join("\n");
          return Text("$listString");
        }

        return Text("暂无数据");
      }),
    );
  }
}
